<!DOCTYPE html>
<html class="writer-html5" lang="Python" >
<head>
  <meta charset="utf-8" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Ganger Causality for Time Series Causal Discovery &mdash; Salesforce CausalAI Library 1.0 documentation</title>
      <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
      <link rel="stylesheet" href="../_static/nbsphinx-code-cells.css" type="text/css" />
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="../_static/jquery.js"></script>
        <script src="../_static/_sphinx_javascript_frameworks_compat.js"></script>
        <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/sphinx_highlight.js"></script>
        <script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
        <script>window.MathJax = {"tex": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true}, "options": {"ignoreHtmlClass": "tex2jax_ignore|mathjax_ignore|document", "processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
        <script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    <script src="../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="VARLINGAM for Time Series Causal Discovery" href="VARLINGAM_Algorithm_TimeSeries.html" />
    <link rel="prev" title="PC algorithm for time series causal discovery" href="PC_Algorithm_TimeSeries.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../index.html" class="icon icon-home">
            Salesforce CausalAI Library
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul>
<li class="toctree-l1"><a class="reference internal" href="Prior%20Knowledge.html">Prior Knowledge</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Data%20objects.html">Data Object</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Data%20Generator.html">Data Generator</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="PC_Algorithm_TimeSeries.html">PC algorithm for time series causal discovery</a></li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="current reference internal" href="#">Ganger Causality for Time Series Causal Discovery</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#Load-and-Visualize-Data">Load and Visualize Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="#Causal-Discovery-(CD)">Causal Discovery (CD)</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#Targeted-Causal-Discovery">Targeted Causal Discovery</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Full-Causal-Discovery">Full Causal Discovery</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="VARLINGAM_Algorithm_TimeSeries.html">VARLINGAM for Time Series Causal Discovery</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="PC_Algorithm_Tabular.html">PC Algorithm for Tabular Causal Discovery</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="GES_Algorithm_Tabular.html">GES for Tabular Causal Discovery</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="LINGAM_Algorithm_Tabular.html">LINGAM for Tabular Causal Discovery</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="GIN_Algorithm_Tabular.html">Generalized Independent Noise (GIN)</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="GrowShrink_Algorithm_Tabular.html">Grow-Shrink Algorithm for Tabular Markov Blanket Discovery</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Benchmarking%20Tabular.html">Benchmark Tabular Causal Discovery Algorithms</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Benchmarking%20TimeSeries.html">Benchmark Time Series Causal Discovery Algorithms</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Causal%20Inference%20Time%20Series%20Data.html">Causal Inference for Time Series</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Causal%20Inference%20Tabular%20Data.html">Causal Inference for Tabular Data</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Salesforce CausalAI Library</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
      <li class="breadcrumb-item active">Ganger Causality for Time Series Causal Discovery</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../_sources/tutorials/GrangerAlgorithm_TimeSeries.ipynb.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="Ganger-Causality-for-Time-Series-Causal-Discovery">
<h1>Ganger Causality for Time Series Causal Discovery<a class="headerlink" href="#Ganger-Causality-for-Time-Series-Causal-Discovery" title="Permalink to this heading"></a></h1>
<p>Granger causality can be used for causal discovery in continuous time series data without contemporaneous causal connections. The intuition behind Granger causality is that for two time series random variables <span class="math notranslate nohighlight">\(X\)</span> and <span class="math notranslate nohighlight">\(Y\)</span>, if including the past values of <span class="math notranslate nohighlight">\(X\)</span> to predict <span class="math notranslate nohighlight">\(Y\)</span> improves the prediction performance, over using only the past values of <span class="math notranslate nohighlight">\(Y\)</span>, then <span class="math notranslate nohighlight">\(X\)</span> causes <span class="math notranslate nohighlight">\(Y\)</span>. In practice, to find the causal parents of a variable, this algorithm involves
performing linear regression to predict that variable using the remaining variables, and using the regression coefficients to determine the causality.</p>
<p>Granger causality assumes: 1. linear relationship between variables, 2. covariance stationary, i.e., a temporal sequence of random variables all have the same mean and the covariance between the random variables at any two time steps depends only on their relative positions, and 3. no hidden confounders.</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[1]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">matplotlib</span>
<span class="kn">from</span> <span class="nn">matplotlib</span> <span class="kn">import</span> <span class="n">pyplot</span> <span class="k">as</span> <span class="n">plt</span>
<span class="o">%</span><span class="k">matplotlib</span> inline
<span class="kn">import</span> <span class="nn">pickle</span> <span class="k">as</span> <span class="nn">pkl</span>
<span class="kn">import</span> <span class="nn">time</span>
<br/></pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[2]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">causalai.models.time_series.granger</span> <span class="kn">import</span> <span class="n">GrangerSingle</span><span class="p">,</span> <span class="n">Granger</span>

<span class="c1"># also importing data object, data transform object, and prior knowledge object, and the graph plotting function</span>
<span class="kn">from</span> <span class="nn">causalai.data.time_series</span> <span class="kn">import</span> <span class="n">TimeSeriesData</span>
<span class="kn">from</span> <span class="nn">causalai.data.data_generator</span> <span class="kn">import</span> <span class="n">DataGenerator</span><span class="p">,</span> <span class="n">GenerateRandomTimeseriesSEM</span>
<span class="kn">from</span> <span class="nn">causalai.data.transforms.time_series</span> <span class="kn">import</span> <span class="n">StandardizeTransform</span>
<span class="kn">from</span> <span class="nn">causalai.models.common.prior_knowledge</span> <span class="kn">import</span> <span class="n">PriorKnowledge</span>
<span class="kn">from</span> <span class="nn">causalai.misc.misc</span> <span class="kn">import</span> <span class="n">plot_graph</span><span class="p">,</span> <span class="n">get_precision_recall</span>
</pre></div>
</div>
</div>
<section id="Load-and-Visualize-Data">
<h2>Load and Visualize Data<a class="headerlink" href="#Load-and-Visualize-Data" title="Permalink to this heading"></a></h2>
<p>Load the dataset and visualize the ground truth causal graph. For the purpose of this example, we will use a synthetic dataset available in our repository.</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[3]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">fn</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span><span class="n">x</span>
<span class="n">coef</span> <span class="o">=</span> <span class="mf">0.1</span>
<span class="n">sem</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="p">[],</span>
        <span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="p">[((</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="n">coef</span><span class="p">,</span> <span class="n">fn</span><span class="p">),</span> <span class="p">((</span><span class="s1">&#39;f&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="n">coef</span><span class="p">,</span> <span class="n">fn</span><span class="p">)],</span>
        <span class="s1">&#39;c&#39;</span><span class="p">:</span> <span class="p">[((</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">),</span> <span class="n">coef</span><span class="p">,</span> <span class="n">fn</span><span class="p">),</span> <span class="p">((</span><span class="s1">&#39;f&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">),</span> <span class="n">coef</span><span class="p">,</span> <span class="n">fn</span><span class="p">)],</span>
        <span class="s1">&#39;d&#39;</span><span class="p">:</span> <span class="p">[((</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">4</span><span class="p">),</span> <span class="n">coef</span><span class="p">,</span> <span class="n">fn</span><span class="p">),</span> <span class="p">((</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="n">coef</span><span class="p">,</span> <span class="n">fn</span><span class="p">),</span> <span class="p">((</span><span class="s1">&#39;g&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="n">coef</span><span class="p">,</span> <span class="n">fn</span><span class="p">)],</span>
        <span class="s1">&#39;e&#39;</span><span class="p">:</span> <span class="p">[((</span><span class="s1">&#39;f&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="n">coef</span><span class="p">,</span> <span class="n">fn</span><span class="p">)],</span>
        <span class="s1">&#39;f&#39;</span><span class="p">:</span> <span class="p">[],</span>
        <span class="s1">&#39;g&#39;</span><span class="p">:</span> <span class="p">[],</span>
        <span class="p">}</span>

<span class="n">T</span> <span class="o">=</span> <span class="mi">5000</span>

<span class="c1"># var_names = [str(i) for i in range(6)]</span>
<span class="c1"># sem = GenerateRandomTimeseriesSEM(var_names=var_names, max_num_parents=2, seed=1)</span>

<span class="n">data_array</span><span class="p">,</span> <span class="n">var_names</span><span class="p">,</span> <span class="n">graph_gt</span> <span class="o">=</span> <span class="n">DataGenerator</span><span class="p">(</span><span class="n">sem</span><span class="p">,</span> <span class="n">T</span><span class="o">=</span><span class="n">T</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="n">graph_gt</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[3]:
</pre></div>
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
{&#39;a&#39;: [],
 &#39;b&#39;: [(&#39;a&#39;, -1), (&#39;f&#39;, -1)],
 &#39;c&#39;: [(&#39;b&#39;, -2), (&#39;f&#39;, -2)],
 &#39;d&#39;: [(&#39;b&#39;, -4), (&#39;b&#39;, -1), (&#39;g&#39;, -1)],
 &#39;e&#39;: [(&#39;f&#39;, -1)],
 &#39;f&#39;: [],
 &#39;g&#39;: []}
</pre></div></div>
</div>
<p>Now we perform the following operations: 1. Standardize the data arrays 2. Create the data object</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[4]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><br/><br/><span></span><span class="c1"># 1.</span>
<span class="n">StandardizeTransform_</span> <span class="o">=</span> <span class="n">StandardizeTransform</span><span class="p">()</span>
<span class="n">StandardizeTransform_</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">data_array</span><span class="p">)</span>

<span class="n">data_trans</span> <span class="o">=</span> <span class="n">StandardizeTransform_</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">data_array</span><span class="p">)</span>

<span class="c1"># 2.</span>
<span class="n">data_obj</span> <span class="o">=</span> <span class="n">TimeSeriesData</span><span class="p">(</span><span class="n">data_trans</span><span class="p">,</span> <span class="n">var_names</span><span class="o">=</span><span class="n">var_names</span><span class="p">)</span>
<br/><br/></pre></div>
</div>
</div>
<p>We visualize the training data below.</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[5]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><br/><br/><span></span><span class="n">plot_graph</span><span class="p">(</span><span class="n">graph_gt</span><span class="p">,</span> <span class="n">node_size</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span>



<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">var_names</span><span class="p">):</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">data_trans</span><span class="p">[</span><span class="o">-</span><span class="mi">50</span><span class="p">:,</span><span class="n">i</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="n">n</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="nboutput docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="../_images/tutorials_GrangerAlgorithm_TimeSeries_8_0.png" src="../_images/tutorials_GrangerAlgorithm_TimeSeries_8_0.png" />
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="../_images/tutorials_GrangerAlgorithm_TimeSeries_8_1.png" src="../_images/tutorials_GrangerAlgorithm_TimeSeries_8_1.png" />
</div>
</div>
</section>
<section id="Causal-Discovery-(CD)">
<h2>Causal Discovery (CD)<a class="headerlink" href="#Causal-Discovery-(CD)" title="Permalink to this heading"></a></h2>
<p>Our library supports running Granger causal discovery in two modes:</p>
<ol class="arabic simple">
<li><p>Targeted CD: Find causal parents of a single given variable. This is useful when we are only interested in finding out the cause of a specific variable, and not others. We thus save both compute and time this way.</p></li>
<li><p>Full CD: Find the full causal graph. This is costlier and scales linearly with the number of variables compared to the time taken by the mode above.</p></li>
</ol>
<p>Enable/Disable Parallel Processing:</p>
<p>Before we instantiate our causal discovery model, we need to decide if we want to use multi-processing. Multi-processing typically provides a significant speed-up for the PC algorithm, but does not provide much advantage for Granger causality because it is already a light weight algorithm, unless the number of variables or samples is very large. In order to use multi-processing in our causal discovery library, we pass the argument use_multiprocessing=True to the model constructor. It’s default
value is False.</p>
<section id="Targeted-Causal-Discovery">
<h3>Targeted Causal Discovery<a class="headerlink" href="#Targeted-Causal-Discovery" title="Permalink to this heading"></a></h3>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[6]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><br/><br/><span></span><span class="n">prior_knowledge</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># PriorKnowledge(forbidden_links={&#39;a&#39;: [&#39;b&#39;]})</span>

<span class="n">target_var</span> <span class="o">=</span> <span class="s1">&#39;b&#39;</span>
<span class="n">max_lag</span> <span class="o">=</span> <span class="mi">4</span>
<span class="n">pvalue_thres</span> <span class="o">=</span> <span class="mf">0.05</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Target Variable: </span><span class="si">{</span><span class="n">target_var</span><span class="si">}</span><span class="s1">, using max_lag </span><span class="si">{</span><span class="n">max_lag</span><span class="si">}</span><span class="s1">, pvalue_thres </span><span class="si">{</span><span class="n">pvalue_thres</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>

<span class="n">granger_single</span> <span class="o">=</span> <span class="n">GrangerSingle</span><span class="p">(</span>
    <span class="n">data</span><span class="o">=</span><span class="n">data_obj</span><span class="p">,</span>
    <span class="n">prior_knowledge</span><span class="o">=</span><span class="n">prior_knowledge</span><span class="p">,</span>
    <span class="n">max_iter</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="c1"># number of optimization iterations for model fitting (default value is 1000)</span>
    <span class="n">use_multiprocessing</span><span class="o">=</span><span class="kc">False</span>
    <span class="p">)</span>
<br/></pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
Target Variable: b, using max_lag 4, pvalue_thres 0.05
</pre></div></div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[7]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">tic</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">granger_single</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">target_var</span><span class="o">=</span><span class="n">target_var</span><span class="p">,</span> <span class="n">pvalue_thres</span><span class="o">=</span><span class="n">pvalue_thres</span><span class="p">,</span> <span class="n">max_lag</span><span class="o">=</span><span class="n">max_lag</span><span class="p">)</span>

<span class="n">toc</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Time taken: </span><span class="si">{</span><span class="n">toc</span><span class="o">-</span><span class="n">tic</span><span class="si">:</span><span class="s1">.2f</span><span class="si">}</span><span class="s1">s</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>

<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">result</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="n">key</span><span class="p">])</span>
    <span class="nb">print</span><span class="p">()</span>
<br/></pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
Time taken: 0.08s

parents
 [(&#39;f&#39;, -1), (&#39;a&#39;, -1)]

value_dict
 {(&#39;a&#39;, -1): 0.06738488546606493, (&#39;a&#39;, -2): 0.0, (&#39;a&#39;, -3): 0.0, (&#39;a&#39;, -4): 0.0, (&#39;b&#39;, -1): 0.0, (&#39;b&#39;, -2): 0.0, (&#39;b&#39;, -3): -0.0, (&#39;b&#39;, -4): -0.0069385872161479584, (&#39;c&#39;, -1): -0.0, (&#39;c&#39;, -2): 0.0, (&#39;c&#39;, -3): 0.0, (&#39;c&#39;, -4): 0.0, (&#39;d&#39;, -1): 0.0, (&#39;d&#39;, -2): 0.0, (&#39;d&#39;, -3): -0.0, (&#39;d&#39;, -4): 0.0, (&#39;e&#39;, -1): -0.0, (&#39;e&#39;, -2): 0.0018099624305629501, (&#39;e&#39;, -3): 0.0, (&#39;e&#39;, -4): 0.0, (&#39;f&#39;, -1): 0.08500800021771439, (&#39;f&#39;, -2): 0.0, (&#39;f&#39;, -3): 0.0, (&#39;f&#39;, -4): 0.0, (&#39;g&#39;, -1): 0.0, (&#39;g&#39;, -2): -0.0, (&#39;g&#39;, -3): -0.0, (&#39;g&#39;, -4): 0.0}

pvalue_dict
 {(&#39;a&#39;, -1): 1.7511628236741997e-06, (&#39;a&#39;, -2): 1.0, (&#39;a&#39;, -3): 1.0, (&#39;a&#39;, -4): 1.0, (&#39;b&#39;, -1): 1.0, (&#39;b&#39;, -2): 1.0, (&#39;b&#39;, -3): 1.0, (&#39;b&#39;, -4): 0.6256123328298464, (&#39;c&#39;, -1): 1.0, (&#39;c&#39;, -2): 1.0, (&#39;c&#39;, -3): 1.0, (&#39;c&#39;, -4): 1.0, (&#39;d&#39;, -1): 1.0, (&#39;d&#39;, -2): 1.0, (&#39;d&#39;, -3): 1.0, (&#39;d&#39;, -4): 1.0, (&#39;e&#39;, -1): 1.0, (&#39;e&#39;, -2): 0.898136948864976, (&#39;e&#39;, -3): 1.0, (&#39;e&#39;, -4): 1.0, (&#39;f&#39;, -1): 1.7218673803622566e-09, (&#39;f&#39;, -2): 1.0, (&#39;f&#39;, -3): 1.0, (&#39;f&#39;, -4): 1.0, (&#39;g&#39;, -1): 1.0, (&#39;g&#39;, -2): 1.0, (&#39;g&#39;, -3): 1.0, (&#39;g&#39;, -4): 1.0}

undirected_edges
 []

</pre></div></div>
</div>
<p>The output variable result is a dictionary with 3 keys, <em>parents</em>, <em>value_dict</em> and <em>pvalue_dict</em>. The first one is a list of the causal parents. Each of latter ones is a dictionary, with keys equal to all possile candidates of the specified target variable. On a side note, note that if any links are specified as forbidden in prior_knowledge, they will be ignored during the computation and will not be present in result.</p>
<p>The dictionary result['value_dict'] contains the strength of the link between the targeted variable and each of the candidate parents. The dictionary result['pvalue_dict'] contains the p-values of the said strength.</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[8]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><br/><span></span><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Predicted parents:&#39;</span><span class="p">)</span>
<span class="n">parents</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="s1">&#39;parents&#39;</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">parents</span><span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Ground truth parents:&quot;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">graph_gt</span><span class="p">[</span><span class="n">target_var</span><span class="p">])</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
Predicted parents:
[(&#39;f&#39;, -1), (&#39;a&#39;, -1)]
Ground truth parents:
[(&#39;a&#39;, -1), (&#39;f&#39;, -1)]
</pre></div></div>
</div>
<p>The way to read this Python dictionary is that <span class="math notranslate nohighlight">\(b[t]\)</span> has parents <span class="math notranslate nohighlight">\(f[t-1]\)</span> and <span class="math notranslate nohighlight">\(a[t-1]\)</span>.</p>
</section>
<section id="Full-Causal-Discovery">
<h3>Full Causal Discovery<a class="headerlink" href="#Full-Causal-Discovery" title="Permalink to this heading"></a></h3>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[9]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><br/><br/><span></span><span class="n">prior_knowledge</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1">#  PriorKnowledge(forbidden_links={&#39;a&#39;: [&#39;b&#39;]})</span>

<span class="n">max_lag</span> <span class="o">=</span> <span class="mi">4</span>
<span class="n">pvalue_thres</span><span class="o">=</span><span class="mf">0.01</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Using max_lag </span><span class="si">{</span><span class="n">max_lag</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>

<span class="n">granger</span> <span class="o">=</span> <span class="n">Granger</span><span class="p">(</span>
        <span class="n">data</span><span class="o">=</span><span class="n">data_obj</span><span class="p">,</span>
        <span class="n">prior_knowledge</span><span class="o">=</span><span class="n">prior_knowledge</span><span class="p">,</span>
        <span class="n">max_iter</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="c1"># number of optimization iterations for model fitting (default value is 1000)</span>
        <span class="n">use_multiprocessing</span><span class="o">=</span><span class="kc">False</span>
        <span class="p">)</span>
<br/></pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
Using max_lag 4
</pre></div></div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[10]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">tic</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">granger</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">pvalue_thres</span><span class="o">=</span><span class="n">pvalue_thres</span><span class="p">,</span> <span class="n">max_lag</span><span class="o">=</span><span class="n">max_lag</span><span class="p">)</span>
<span class="n">toc</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Time taken: </span><span class="si">{</span><span class="n">toc</span><span class="o">-</span><span class="n">tic</span><span class="si">:</span><span class="s1">.2f</span><span class="si">}</span><span class="s1">s</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>


<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39; The output causal_strength has keys: </span><span class="si">{</span><span class="n">result</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
Time taken: 0.56s

 The output causal_strength has keys: dict_keys([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;])
</pre></div></div>
</div>
<p>The output <em>result</em> has the variable names as its keys, and the value corresponding to each key is a dictionary with 3 keys, parents, value_dict and pvalue_dict. The first one is a list of the causal parents. The dictionary result['value_dict'] contains the strength of the link between the targeted variable and each of the candidate parents. The dictionary result['pvalue_dict'] contains the p-values of the said strength.</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[11]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Ground truth parents:&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">graph_gt</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
    <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">key</span><span class="si">}</span><span class="s1">: </span><span class="si">{</span><span class="n">graph_gt</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Predicted parents:&#39;</span><span class="p">)</span>
<span class="n">graph_est</span><span class="o">=</span><span class="p">{</span><span class="n">n</span><span class="p">:[]</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">result</span><span class="o">.</span><span class="n">keys</span><span class="p">()}</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">result</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
    <span class="n">parents</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="n">key</span><span class="p">][</span><span class="s1">&#39;parents&#39;</span><span class="p">]</span>
    <span class="n">graph_est</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">parents</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">key</span><span class="si">}</span><span class="s1">: </span><span class="si">{</span><span class="n">parents</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>


<span class="n">precision</span><span class="p">,</span> <span class="n">recall</span><span class="p">,</span> <span class="n">f1_score</span> <span class="o">=</span> <span class="n">get_precision_recall</span><span class="p">(</span><span class="n">graph_est</span><span class="p">,</span> <span class="n">graph_gt</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Precision </span><span class="si">{</span><span class="n">precision</span><span class="si">:</span><span class="s1">.2f</span><span class="si">}</span><span class="s1">, Recall: </span><span class="si">{</span><span class="n">recall</span><span class="si">:</span><span class="s1">.2f</span><span class="si">}</span><span class="s1">, F1 score: </span><span class="si">{</span><span class="n">f1_score</span><span class="si">:</span><span class="s1">.2f</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
Ground truth parents:
a: []
b: [(&#39;a&#39;, -1), (&#39;f&#39;, -1)]
c: [(&#39;b&#39;, -2), (&#39;f&#39;, -2)]
d: [(&#39;b&#39;, -4), (&#39;b&#39;, -1), (&#39;g&#39;, -1)]
e: [(&#39;f&#39;, -1)]
f: []
g: []

Predicted parents:
a: []
b: [(&#39;f&#39;, -1), (&#39;a&#39;, -1)]
c: [(&#39;b&#39;, -2), (&#39;f&#39;, -2)]
d: [(&#39;g&#39;, -1), (&#39;b&#39;, -4), (&#39;b&#39;, -1)]
e: [(&#39;f&#39;, -1)]
f: []
g: []
Precision 1.00, Recall: 1.00, F1 score: 1.00
</pre></div></div>
</div>
<p>The way to read this Python dictionary is that <span class="math notranslate nohighlight">\(a[t]\)</span> has no parent. <span class="math notranslate nohighlight">\(b[t]\)</span> has parents <span class="math notranslate nohighlight">\(f[t-1]\)</span> and <span class="math notranslate nohighlight">\(a[t-1]\)</span>, and so on.</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[ ]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span>
</pre></div>
</div>
</div>
</section>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="PC_Algorithm_TimeSeries.html" class="btn btn-neutral float-left" title="PC algorithm for time series causal discovery" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="VARLINGAM_Algorithm_TimeSeries.html" class="btn btn-neutral float-right" title="VARLINGAM for Time Series Causal Discovery" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2022, salesforce.com, inc..</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>